package com.example.hello.service.impl;

import com.example.hello.mapper.BookMapper;
import com.example.hello.mapper.InteractionMapper;
import com.example.hello.pojo.Book;
import com.example.hello.pojo.Interaction;
import com.example.hello.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CollectionServiceImpl implements CollectionService {

    @Autowired
    private InteractionMapper interactionMapper;
    
    @Autowired
    private BookMapper bookMapper;

    @Override
    public List<Book> getFavoriteBooks(Integer userId) {
        // 查询 user_book_interaction 表中该用户的收藏记录
        List<Interaction> interactions = interactionMapper.getFavoriteInteractionsByUser(userId);
        List<Book> result = new ArrayList<>();
        if (interactions != null) {
            // 对于每一条记录，根据 bookId 查询图书详情
            for (Interaction interaction : interactions) {
                Book book = bookMapper.getBookById(interaction.getBookId());
                if (book != null) {
                    result.add(book);
                }
            }
        }
        return result;
    }
}